#include <stdio.h>
#include <stdlib.h>

#include "Common.h"
#include "Window.h"
#include "Internals.h"
#include "CtrlClass.h"
#include "Static.h"

#include "Assert.h"

/* Text alignment flags, horizontal */
#define GUI_TA_HORIZONTAL  (3<<0)
#define GUI_TA_LEFT        (0<<0)
#define GUI_TA_RIGHT	     (1<<0)
#define GUI_TA_CENTER	     (2<<0)
#define GUI_TA_HCENTER	   GUI_TA_CENTER  /* easier to remember :-)  */

/* Text alignment flags, vertical */
#define GUI_TA_VERTICAL   (3<<2)
#define GUI_TA_TOP	      (0<<2)
#define GUI_TA_BOTTOM	    (1<<2)
#define GUI_TA_BASELINE   (2<<2)
#define GUI_TA_VCENTER    (3<<2)

static int StaticControlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam);

BOOL RegisterStaticControl (void)
{
    WNDCLASS WndClass;

    WndClass.spClassName = CTRL_STATIC;
    WndClass.dwStyle     = WS_NONE;
    WndClass.WinProc     = StaticControlProc;

    return AddNewControlClass (&WndClass) == ERR_OK;
}

static int StaticControlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    RECT        rcClient;
    const char* spCaption;
    PCONTROL    pCtrl;
    UINT        uFormat;    
    DWORD       dwStyle;

    pCtrl = Control (hWnd);

    switch(message)
    {
        case MSG_CREATE:
        return 0;
        case MSG_PAINT:            
            //LG_ASSERT(0);
            GetClientRect(hWnd, &rcClient);
            dwStyle = GetWindowStyle (hWnd);

            switch (dwStyle & SS_TYPEMASK)
            {
                case SS_LEFT:
                    uFormat = GUI_TA_LEFT | GUI_TA_VCENTER;
                break;
                case SS_CENTER:
                    uFormat = GUI_TA_CENTER | GUI_TA_VCENTER;
                break;
                case SS_RIGHT:
                    uFormat = GUI_TA_RIGHT | GUI_TA_VCENTER;
                break;
            }
            
            //LG_ASSERT(0);
            GUIClearRect(rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
            spCaption = pCtrl->spCaption;
            GUI_DispStringInRect(spCaption, &rcClient, uFormat);
            //LG_ASSERT(0);
            //Uart0_Printf("MSG_PAINT %d  %d  %d  %d\r\n", rcClient.x0, rcClient.y0, rcClient.x1, rcClient.y1);
            //GUI_Context.pDeviceAPI->pfDrawRect(rcClient.x0, rcClient.y0, rcClient.x1, rcClient.y1);
        break;
        case MSG_SETTEXT:
            //pCtrl = GetControl(hWnd);
            if((char*)lParam == NULL || pCtrl == NULL)
            {
                LG_ASSERT(0);
                return -1;
            }

            LG_ASSERT(0);
            
            if(strcmp(pCtrl->spCaption, (char*)lParam))
            {
                LG_ASSERT(0);
                SetWindowCaption (hWnd, (char*)lParam);
                PostMessage (hWnd, MSG_PAINT, 0, 0);
            }
         break;
    }
    
    return DefaultControlProc(hWnd, message,wParam, lParam);
}
